﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing.Imaging;
using System.Drawing;
using GeckoPhotos.Tests.Support;

namespace GeckoPhotos.Tests.Util
{
    [TestClass]
    public class ImageUtil
    {

        [TestMethod]
        public void GetMimeTypeExpectedCommonTypes()
        {
            // To build this test, I just observed the GetMimeType() output of all possible
            //  values of the ImageFormat enum.

            Assert.AreEqual("image/bmp", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Bmp));
            Assert.AreEqual("image/x-emf", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Emf));
            Assert.AreEqual("image/unknown", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Exif));
            Assert.AreEqual("image/gif", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Gif));
            Assert.AreEqual("image/x-icon", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Icon));
            Assert.AreEqual("image/jpeg", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Jpeg));
            Assert.AreEqual("image/unknown", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.MemoryBmp));
            Assert.AreEqual("image/png", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Png));
            Assert.AreEqual("image/tiff", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Tiff));
            Assert.AreEqual("image/x-wmf", GeckoPhotos.Util.ImageUtil.GetMimeType(ImageFormat.Wmf));
        }

        [TestMethod]
        public void GetImageAsBytes()
        {
            Bitmap sourceImage = Properties.Resources.Moon32x32;
            byte[] expectedBytes = ImageResourceBytes.Moon32x32PngBytes;

            var result = GeckoPhotos.Util.ImageUtil.GetImageAsBytes(sourceImage, ImageFormat.Png);

            Assert.IsTrue(expectedBytes.SequenceEqual(result));
        }

        [TestMethod]
        public void CreateResizedImageToFitWidthHeightCorrrectlyResizesMoon64x64()
        {
            var result = GeckoPhotos.Util.ImageUtil.CreateResizedImageToFitWidthHeight(
                16,
                16,
                ImageResourceBytes.Moon64x64PngBytes,
                ImageFormat.Png
                );

            Assert.IsTrue(ImageResourceBytes.Moon64x64Thumbnail16x16PngBytes.SequenceEqual(result));
        }

        [TestMethod]
        public void CreateResizedImageToFitWidthHeightCorrrectlyResizesMoon64x64WithWideThumbnail()
        {
            var result = GeckoPhotos.Util.ImageUtil.CreateResizedImageToFitWidthHeight(
                1000,
                16,
                ImageResourceBytes.Moon64x64PngBytes,
                ImageFormat.Png
                );

            Assert.IsTrue(ImageResourceBytes.Moon64x64Thumbnail16x16PngBytes.SequenceEqual(result));
        }

        [TestMethod]
        public void CreateResizedImageToFitWidthHeightCorrrectlyResizesMoon64x64WithTallThumbnail()
        {
            var result = GeckoPhotos.Util.ImageUtil.CreateResizedImageToFitWidthHeight(
                16,
                1000,
                ImageResourceBytes.Moon64x64PngBytes,
                ImageFormat.Png
                );

            Assert.IsTrue(ImageResourceBytes.Moon64x64Thumbnail16x16PngBytes.SequenceEqual(result));
        }

        [TestMethod]
        public void CreateResizedImageToFitWidthHeightDoesntAlwaysReturn16x16Result()
        {
            var result = GeckoPhotos.Util.ImageUtil.CreateResizedImageToFitWidthHeight(
                17,
                18,
                ImageResourceBytes.Moon64x64PngBytes,
                ImageFormat.Png
                );

            Assert.IsFalse(ImageResourceBytes.Moon64x64Thumbnail16x16PngBytes.SequenceEqual(result));
        }

    }
}
